<?php

class AccountController extends Zend_Controller_Action {
	
	protected function _getAuthAdapter($userName, $userPassword) {
		$registry = Zend_Registry::getInstance();
		$authAdapter = new Zend_Auth_Adapter_DbTable ( 
			$registry->dbAdapter,
			'users',
			'login',
			'password');
		$authAdapter->setIdentity ( $userName )->setCredential ( $userPassword );	
		return $authAdapter;
	}
	
	public function indexAction() {
        $registry = Zend_Registry::getInstance();
        if($registry->currentUser instanceof Application_Model_User &&
            $registry->currentUser->get('role') != "guest"){
            $this->_redirect(   $this
                                        ->view
                                        ->url(array('controller' => 'office',
                                                    'action' => 'index')));
                    return;
        }
        
        $formLogin = new Application_Form_Login();
		$request = $this->getRequest();
		if ($request->isPost()) {
            $postData = $request->getPost();  
			if ($formLogin->isValid($request->getPost())) {
                $auth = Zend_Auth::getInstance ();
				$authAdapter = $this->_getAuthAdapter ( $postData ['login'], $postData ['password'] );
				$result = $auth->authenticate ($authAdapter);
                if ($result->isValid ()) {
					$currentUserData = $authAdapter->getResultRowObject();
                    $usersService = Application_Model_UsersService::getInstance();
                    $currentUser = $usersService->mapToObject((array)$currentUserData);
					$_uni_session_code = substr(md5(uniqid(rand(),true)), -32);
					$currentUser->set('uni_session_code', $_uni_session_code);
                    $currentUser->set('date_last_entry', time());
                    $usersService->save($currentUser);
                    $currentUser->set('date_last_entry', time());
					$registry->currentUser = $currentUser;
					setcookie("cold", $_uni_session_code , time()+3600, '/');
                    $this->_redirect(   $this
                                        ->view
                                        ->url(array('controller' => 'office',
                                                    'action' => 'index')));
                    return;
                }
			}
            $formLogin->populate ( $postData );
        }
        $this->view->formLogin = $formLogin;
	}
	
	public function unloginAction()
	{
		$registry = Zend_Registry::getInstance();
		if ($registry->currentUser->get('role') != "guest")
		{
			$usersService = Application_Model_UsersService::getInstance();
			$currentUser = $registry->currentUser;
			$currentUser->set('uni_session_code', "");
			$usersService->save($currentUser);
			$registry->currentUser = null;
            setcookie("cold", "" , 0 , '/');
		}
		$this->_redirect(   $this
                            ->view
                            ->url(array('controller' => 'account',
                                        'action' => 'index')));
        return;
	}
}

